package com.chuangke.attachment.service.impl;

import com.chuangke.system.entity.SysAttachment;
import com.chuangke.system.service.SysAttachmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.chuangke.attachment.constant.AttachmentConstanst;
import com.chuangke.attachment.service.AttachmentService;
import com.chuangke.common.constant.FileConstants;
import com.chuangke.common.constant.SysRelationTypeConstants;
import com.chuangke.common.exception.ChuangkeException;
import com.chuangke.file.model.FileInfo;
import com.chuangke.file.service.FileInfoService;
import com.chuangke.file.service.FileTransferService;
import com.chuangke.system.entity.SysRelation;
import com.chuangke.system.service.SysRelationService;

import cn.hutool.core.util.StrUtil;

@Service
public class AttachmentServiceImpl implements AttachmentService {

	@Autowired
	private SysRelationService sysRelationService;
	@Autowired
	private FileTransferService fileTransferService;

	@Autowired
	private FileInfoService fileInfoService;

	@Autowired
	private SysAttachmentService sysAttachmentService;

	@Override
	public FileInfo findAttachmentMeta(String attachmentId) {
		return fileInfoService.findFileInfo(attachmentId);
	}

	@Override
	public void deleteAttachmentMeta(String attachmentId) {
		fileInfoService.deleteFileInfo(attachmentId);
	}

	@Override
	public FileInfo uploadAchievement(String id, MultipartFile file) {
		FileInfo fileInfo = fileTransferService.upload(file, FileConstants.SYSTEM_DBANK_ID);

		if (!StrUtil.isBlank(id) && !AttachmentConstanst.NO_ID_UPLOAD.equals(id)) {
			sysRelationService.save(id, SysRelationTypeConstants.AFFIX, fileInfo.getId());
		}

		return fileInfo;
	}

	@Override
	public FileInfo uploadAchievement(SysRelation relation, MultipartFile file) {
		FileInfo fileInfo = fileTransferService.upload(file, FileConstants.SYSTEM_DBANK_ID);
		relation.setRelationId(fileInfo.getId());
		sysRelationService.save(relation);

		return fileInfo;
	}

	@Override
	public byte[] previewThum(String attachmentId) {
		FileInfo fileInfo = this.findAttachmentMeta(attachmentId);
		if (fileInfo == null) {
			return null;
		}

        return fileTransferService.images(fileInfo.getStorageType(), fileInfo.getThumStorage1(),
				fileInfo.getThumStorage2());
	}

	@Override
	public byte[] download(String attachmentId) {
		FileInfo fileInfo = this.findAttachmentMeta(attachmentId);
		if (fileInfo == null) {
			throw new ChuangkeException("未找到文件");
		}
//		byte[] fileBytes = fileTransferService.download(fileInfo.getId(), fileInfo.getSignId()) ;
        return fileTransferService.justDownload(fileInfo.getStorageType(), fileInfo.getStorage1(),
				fileInfo.getStorage2());
	}

	@Override
	public void delete(String bisId, String attachmentId) {
		if (!StrUtil.isBlank(bisId) && !AttachmentConstanst.NO_ID_UPLOAD.equals(bisId)) {
			sysRelationService.deleteBySubjectId(bisId);
		}
		this.deleteAttachmentMeta(attachmentId);
	}

	@Override
	public FileInfo uploadSysAchievement(String id, MultipartFile file) {
		FileInfo fileInfo = fileTransferService.upload(file, FileConstants.SYSTEM_DBANK_ID);
		if (!StrUtil.isBlank(id) && !AttachmentConstanst.NO_ID_UPLOAD.equals(id)) {
			SysAttachment sysAttachment = new SysAttachment();
			sysAttachment.setId(fileInfo.getId());
			sysAttachment.setSubjectId(id);
			sysAttachment.setName(fileInfo.getName());
			sysAttachment.setStatus("1");
			sysAttachmentService.save(sysAttachment);
		}
		return fileInfo;
	}

}
